import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_form_core/theme/base_form_theme.dart';

/// radio 控件
class BaseFormAnOtherRadio extends StatefulWidget {
  final List<BaseKeyValueModel> radioList;

  final dynamic selectedKey;

  final Function(BaseKeyValueModel) callBack;

  const BaseFormAnOtherRadio({Key key, @required this.radioList, @required this.selectedKey, this.callBack})
      : assert(radioList != null),
        super(key: key);

  @override
  _BaseFormAnOtherRadioState createState() => _BaseFormAnOtherRadioState();
}

class _BaseFormAnOtherRadioState extends State<BaseFormAnOtherRadio> {
  dynamic _selectedKey;

  @override
  void initState() {
    _selectedKey = widget.selectedKey;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.radioList.length == 0) return SizedBox();
    return Container(
      height: 48,
      child: ListView.separated(
        shrinkWrap: true,
        scrollDirection: Axis.horizontal,
        itemBuilder: (context, index) => buildPickItem(widget.radioList[index]),
        separatorBuilder: (_, __) => SizedBox(width: 24),
        itemCount: widget.radioList.length,
      ),
    );
  }

  Widget buildPickItem(BaseKeyValueModel item) {
    Color primaryColor = Theme.of(context).primaryColor == Colors.white ? Theme.of(context).textTheme.bodyText2.color : Theme.of(context).primaryColor;
    bool isSelected = item.key == _selectedKey;
    Color textColor = isSelected ? primaryColor : Theme.of(context).textTheme.bodyText2.color;
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () => _onSelect(item),
      child: Container(
        height: 24,
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            CustomPaint(size: Size(18, 18), painter: PickItemCustomPainter(isSelected: isSelected, selectBGColor: primaryColor, selectRingColor: primaryColor, notSelectRingColor: BaseFormTheme.borderColor)),
            SizedBox(width: 4),
            Text(item.value, softWrap: false, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 13, color: textColor)),
          ],
        ),
      ),
    );
  }

  void _onSelect(BaseKeyValueModel item) {
    if (_selectedKey != item.key) {
      setState(() {
        _selectedKey = item.key;
        if (widget.callBack != null) widget.callBack(item);
      });
    }
  }
}

class PickItemCustomPainter extends CustomPainter {
  final bool isSelected;
  final Color selectRingColor;
  final Color notSelectRingColor;
  final Color selectBGColor;
  final Color notSelectBGColor;
  final bool showHook;
  final Color hookColor;

  PickItemCustomPainter({@required this.isSelected, @required this.selectRingColor, this.notSelectRingColor = BaseBodyTheme.iconColor, @required this.selectBGColor, this.notSelectBGColor = Colors.white, this.showHook = true, this.hookColor = Colors.white});

  Paint ringPaint = Paint()
    ..style = PaintingStyle.stroke
    ..strokeWidth = 1.5;

  Paint bGPaint = Paint()..style = PaintingStyle.fill;
  Paint hookPaint = Paint()..style = PaintingStyle.fill;

  @override
  void paint(Canvas canvas, Size size) {
    if (isSelected == null) return;
    // 绘制圆环
    drawRing(canvas, size);
    // 绘制背景
    drawBG(canvas, size);
    // 绘制勾
    if (showHook) drawHook(canvas, size);
  }

  /// 绘制圆环
  drawRing(Canvas canvas, Size size) {
    canvas.save();
    canvas.translate(size.width / 2, size.height / 2);
    if (!this.isSelected) canvas.drawCircle(Offset.zero, 8, ringPaint..color = notSelectRingColor);
    if (this.isSelected) canvas.drawCircle(Offset.zero, 8, ringPaint..color = selectRingColor);
    canvas.restore();
  }

  /// 绘制背景
  drawBG(Canvas canvas, Size size) {
    canvas.save();
    canvas.translate(size.width / 2, size.height / 2);
    if (!this.isSelected) canvas.drawCircle(Offset.zero, 7, bGPaint..color = notSelectBGColor);
    if (this.isSelected) canvas.drawCircle(Offset.zero, 8, bGPaint..color = selectBGColor);
    canvas.restore();
  }

  /// 绘制勾
  drawHook(Canvas canvas, Size size) {
    canvas.save();
    Paint _paint = Paint()
      ..color = this.hookColor
      ..strokeWidth = 2
      ..style = PaintingStyle.stroke;
    Path _path = Path();
    _path.moveTo(size.width / 4, size.height / 2);
    _path..lineTo(size.width * 2 / 5, size.height * 0.7);
    _path..lineTo(size.width * 0.8, size.height * 3 / 10);
    canvas.drawPath(_path, _paint);
    canvas.restore();
  }

  @override
  bool shouldRepaint(covariant PickItemCustomPainter old) => old.isSelected != isSelected;
}
